package com.wuxinwudai.android.app;


import com.wuxinwudai.adlg.ProgressDialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.ViewGroup;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;

/**
 * WebViewActivity 类主要用于加载资源 html 页面，跳转时只需在 Intent 传递 URL 和 CACHE_MODE 两个参数
 * @author 吾心无待 于2016年03月01日
 */
public final class WebViewActivity extends LinearLayoutActivity {
    private WebView webView;
    private ProgressDialog progressDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int cacheMode = getIntent().getIntExtra("CACHE_MODE", WebSettings.LOAD_CACHE_ELSE_NETWORK);
        String url = getIntent().getStringExtra("URL");
        tvTitle.setText(getIntent().getStringExtra("TITLE"));
        webView.getSettings().setCacheMode(cacheMode);
        progressDialog = new ProgressDialog(WebViewActivity.this);
        progressDialog.setProgressText("加载中...");
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                webView.loadUrl(url);
                return true;
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                progressDialog.show();
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                progressDialog.dismiss();
            }

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);
                progressDialog.dismiss();
            }
        });
        webView.loadUrl(url);
    }

    @Override
    protected void setChildView() {
        webView = new WebView(this);
        webView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        lyContainer.addView(webView);
    }

    @Override
    public int getLayoutId() {
        return 0;
    }

    @Override
    public void onBackPressed() {
        if (webView.canGoBack()){
            webView.goBack();
        } else {
            super.onBackPressed();
        }
    }

}
